﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework.Repository.Extensions
{
    /// <summary>
    /// 扩展IQueryable
    /// </summary>
    public static class QueryableExtensions
    {
        /// <summary>
        ///使IQueryable支持QueryModel
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="table">IQueryable的查询对象</param>
        /// <param name="model">QueryModel对象</param>
        /// <returns></returns>
        public static IQueryable<T> Where<T>(this IQueryable<T> table, QueryCondition model) where T : class
        {
            Contract.Requires(table != null);
            return Where<T>(table, model.FilterList);
        }

        private static IQueryable<T> Where<T>(IQueryable<T> table, IEnumerable<QueryFilter> items)
        {
            Contract.Requires(table != null);
            if (items.Count() == 0) return table;
            return new QueryableSearcher<T>(table, items).Search();
        }

    }
}
